<template>
  <base-page v-bind="baseInfo">
    <template #searchBar>
      <filter-form
        ref="filterForm"
        :elements="elements"
        noimmediate
        @submit="handleSearch"
        @reset="handlerReset"
      >
        <Button icon="el-icon-receiving" type="plain" @click="handleFile">归档</Button>
        <Button
          icon="icon-daochu"
          :btn-loading="dcLoading"
          @click="handleDealExcel"
        >导出</Button>
      </filter-form>
    </template>
    <template #content>
      <div class="table-box">
        <Table
          ref="tableref"
          :columns="tableColumns"
          :loading="tableLoading"
          :data="tableData"
          :hide-pagination="true"
          :show-summary="true"
          :summary-method="getSummaries"
        >
          <template #scale="{row}">
            {{ (row.scale * 100).toFixed(2) }}%
          </template>
        </Table>
      </div>
    </template>
  </base-page>
</template>
<script>
import {
  getAssessmentCyclePull
  // getExport
} from '@/api/weight'
import {
  getListByPeriod,
  isFile,
  exportData
} from '@/api/dataCleaning'
import {
  getAssessmentCycleById
} from '@/api/targetManagement'
export default {
  data() {
    return {
      baseInfo: {
        searchBarShow: true
      },
      tableColumns: [
        {
          title: '核算单元名称',
          dataIndex: 'accountingName'
        },
        {
          title: '核算单元编码',
          dataIndex: 'accountingCode'
        },
        {
          title: '核算单元权重',
          dataIndex: 'workloadWeight'
        },
        {
          title: '初始分值',
          dataIndex: 'initialScore'
        },
        {
          title: '导管工作量',
          dataIndex: 'ductScore'
        },
        {
          title: '督查扣分',
          dataIndex: 'supervisionScore'
        },
        {
          title: '内镜手术',
          dataIndex: 'opScore'
        },
        {
          title: '人工统计',
          dataIndex: 'manualScore'
        },
        // {
        //   title: '工作量权重',
        //   dataIndex: ''
        // },
        {
          title: '季度合计',
          dataIndex: 'totalScore'
        },
        {
          title: '人员编制',
          dataIndex: 'staffCount'
        },
        {
          title: '编制均分',
          dataIndex: 'avgSorce'
        },
        {
          title: '上个周期',
          dataIndex: 'lastCount'
        },
        {
          title: '上升比',
          dataIndex: 'scale',
          type: 'otherTag'
        }
      ],
      tableLoading: false,
      dcLoading: false,
      tableData: [],
      newCondition: {},
      elements: [
        {
          label: '周期',
          key: 'cycle',
          type: 'select',
          itemLabel: 'periodPull',
          itemValue: 'periodSuffix',
          hideSelectAll: true,
          options: [],
          value: ''
        }
      ],
      imageShow: false
    }
  },
  created() {
    this.getSelect()
  },
  mounted() {},
  methods: {
    getFileInfo() {
      const id = this.elements[0].options.find(item => item.periodSuffix === this.newCondition?.cycle)?.id
      getAssessmentCycleById({
        id: id
      }).then((res) => {
        if (res.code === 200) {
          const { isFile } = res.data
          if (isFile === 1) {
            this.imageShow = true
          } else {
            this.imageShow = false
          }
        }
      })
    },
    getSelect() {
      // 周期
      getAssessmentCyclePull().then((res) => {
        if (res.code === 200) {
          this.elements[0].options = res.data
          const { periodSuffix } = res.data[0]
          this.elements[0].value = periodSuffix
          localStorage.setItem(
            'cycle',
            JSON.stringify(this.elements[0].options)
          )
          this.handleSearch(this.$refs.filterForm.getParms())
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 查询
    handleSearch(value) {
      this.newCondition = value
      this.getData()
      this.getFileInfo()
    },
    // 重置
    handlerReset() {
      this.handleSearch()
    },
    getData() {
      const params = {
        period: this.newCondition?.cycle
        // id: this.newCondition?.cycle,
        // deptCode: this.newCondition.comprehensiveSearch
      }
      this.tableLoading = true
      getListByPeriod(params)
        .then((res) => {
          this.tableLoading = false
          if (res.code === 200) {
            this.tableData = res.data
            this.$nextTick(() => {
              this.$refs.tableref.doLayout()
            })
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch(() => {
          this.tableLoading = false
        })
    },
    // 归档
    handleFile() {
      this.$ckConfirm({
        content: '是否将本次绩效归档，并下发至科室？',
        title: '提示',
        okFn: res => {
          const param = {
            id: this.elements[0].options.find(item => item.periodSuffix === this.$refs.filterForm.getParms()?.cycle)?.id
          }
          isFile(param).then(res => {
            if (res.code === 200) {
              this.$message.success(res.msg)
            } else {
              this.$message.error(res.msg)
            }
          })
        },
        cancelFn: res => {
          console.log('取消')
        }
      })
    },
    handleDealExcel() {
      // 导出
      const params = {
        period: this.newCondition?.cycle
        // deptCode: this.newCondition.comprehensiveSearch
        // ids: this.selections.map((item) => item.id)
      }
      this.dcLoading = true
      exportData(params).then((res) => {
        this.dcLoading = false
        this.$utils.downloadFileFun(res, '工作量指标分值.xls')
      })
    },
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        }
        if (column.property === 'initialScore') {
          const values = data.map(item => Number(item[column.property]))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            sums[index] = sums[index].toFixed(4)
          } else {
            sums[index] = 'N/A'
          }
        }
        if (column.property === 'ductScore') {
          const values = data.map(item => Number(item[column.property]))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            sums[index] = sums[index].toFixed(4)
          } else {
            sums[index] = 'N/A'
          }
        }
        if (column.property === 'supervisionScore') {
          const values = data.map(item => Number(item[column.property]))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            sums[index] = sums[index].toFixed(4)
          } else {
            sums[index] = 'N/A'
          }
        }
        if (column.property === 'opScore') {
          const values = data.map(item => Number(item[column.property]))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            sums[index] = sums[index].toFixed(4)
          } else {
            sums[index] = 'N/A'
          }
        }
        if (column.property === 'manualScore') {
          const values = data.map(item => Number(item[column.property]))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            sums[index] = sums[index].toFixed(4)
          } else {
            sums[index] = 'N/A'
          }
        }
        if (column.property === 'totalScore') {
          const values = data.map(item => Number(item[column.property]))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            sums[index] = sums[index].toFixed(4)
          } else {
            sums[index] = 'N/A'
          }
        }
        if (column.property === 'staffCount') {
          const values = data.map(item => Number(item[column.property]))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            sums[index] = sums[index].toFixed(4)
          } else {
            sums[index] = 'N/A'
          }
        }
      })

      return sums
    }
  }
}
</script>
<style lang="less" scoped>
.table-box{
  height: 100%;
  display: flex;
  flex-direction: column;
}
</style>
